package com.xinzhi.controller;

import com.xinzhi.service.BankServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

/**
 * BankController 是一个处理银行账户操作请求的控制器。
 */
public class BankController{
    @RestController
    @RequestMapping("/api/bank")
    public class BankApiController {

        @Autowired
        private BankServiceImpl.BankAccount bankAccount;

        /**
         * 存款接口
         * @param amount 存款金额
         * @return  返回存款结果的字符串
         */
        @PostMapping("/deposit")
        public String deposit(@RequestParam double amount) {
            try {
                bankAccount.deposit(amount);
                return "存款成功，\n存入金额：" + amount + "，当前余额：" + bankAccount.getBalance();
            } catch (IllegalArgumentException e) {
                return "存款失败：" + e.getMessage();
            }
        }

        /**
         * 提现接口
         * @param amount 提现金额
         * @return  返回提现结果的字符串
         */
        @PostMapping("/withdraw")
        public String withdraw(@RequestParam double amount) {
            try {
                bankAccount.withdraw(amount);

                return "提现成功，提现金额：" + amount + "，当前余额：" + bankAccount.getBalance();
            } catch (IllegalArgumentException e) {
                return "提现失败：" + e.getMessage();
            }
        }
    }


}

